﻿using MVC_Project.BLL;
using Project.DAL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Project.BLL
{
    public class customerBll: BaseBll<customer>
    {
        customerDal dal = new customerDal();
        public override void SetDal()
        {
            Dal = new customerDal();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<customer> GetCustomerByPage(string name, int page, int size, out int count)
        {
            var cust = base.Query();

            if (!string.IsNullOrWhiteSpace(name))
            {
                cust = cust.Where(u => u.nickname.Contains(name));
            }
            count = cust.Count();

            var data = cust.OrderBy(u => u.CId).Skip((page - 1) * size).Take(size);

            return data.ToList();
        }

        public bool UpdateCust(customer users)
        {
            var user = Dal.Query().Where(u => u.CId == users.CId).FirstOrDefault();
            if (user != null)
            {
                user.nickname = users.nickname;
                user.mobilePhone = users.mobilePhone;
                user.loginPwd = users.loginPwd;
                user.gender = users.gender;
                user.address = users.address;

                Dal.Update(user);

                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

        public bool DeleteCust(int CId)
        {
            var user = Dal.Query().Where(u => u.CId == CId).FirstOrDefault();
            if (user != null)
            {
                Dal.Delete(user);
                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

        public bool DeleteCust(int[] CId)
        {
            var users = Dal.Query().Where(u => CId.Contains(u.CId)).ToList();
            if (users != null)
            {
                Dal.Delete(users);
                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

    }
}
